package com.wolf.cx330.base

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.AnimatedVectorDrawable
import android.util.TypedValue
import android.widget.ImageView
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
import com.wolf.cx330.R
import java.util.*

fun animateView(view: ImageView) {
    when (val drawable = view.drawable) {
        is AnimatedVectorDrawableCompat -> {
            drawable.start()
        }
        is AnimatedVectorDrawable -> {
            drawable.start()
        }
    }
}

/**
 * 获取随机颜色 [如果是用的别人的方法 最好是附上连接]
 * https://blog.csdn.net/aicpzl/article/details/46301519
 */
fun getRandColorCode(): String {
    val random = Random()
    var r = Integer.toHexString(random.nextInt(256)).toUpperCase(Locale.ROOT)
    var g = Integer.toHexString(random.nextInt(256)).toUpperCase(Locale.ROOT)
    var b = Integer.toHexString(random.nextInt(256)).toUpperCase(Locale.ROOT)
    r = if (r.length == 1) "0$r" else r
    g = if (g.length == 1) "0$g" else g
    b = if (b.length == 1) "0$b" else b
    return "#$r$g$b"
}

fun Float.dp() = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    this,
    Resources.getSystem().displayMetrics
)

fun Int.dp() = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    this.toFloat(),
    Resources.getSystem().displayMetrics
)

fun Resources.getAvatar(width: Int): Bitmap {
    val option = BitmapFactory.Options()
    option.inJustDecodeBounds = true
    BitmapFactory.decodeResource(this, R.drawable.icon_user, option)
    option.apply {
        inJustDecodeBounds = false
        inDensity = option.outWidth
        inTargetDensity = width
    }
    return BitmapFactory.decodeResource(this, R.drawable.icon_user, option)


}

fun getZForCamera(): Float {
    return -4 * Resources.getSystem().displayMetrics.density
}